import datetime
import json

from django.http import JsonResponse
from django.shortcuts import render
from django.views import View

# POST /api/v1.0/orders
from ihome.utils.mixins import LoginRequiredMixin

from homes.models import House
from order.models import Order


class OrderInfo(LoginRequiredMixin, View):

    def post(self, request):
        result = json.loads(request.body.decode())
        house_id = result.get('house_id')
        start_date = result.get('start_date')
        end_date = result.get('end_date')
        if not all({house_id, start_date, end_date}):
            return JsonResponse({
                "errno": "400",
                "errmsg": "缺少必传参数"
            })
        d1 = datetime.datetime(int(start_date[:3]), int(start_date[5:7]), int(start_date[8:]))
        d2 = datetime.datetime(int(end_date[:3]), int(end_date[5:7]), int(end_date[8:]))
        days = d2- d1
        days = days.days
        user = request.user
        try:
            house = House.objects.get(id=house_id)
        except Exception as e:
            return JsonResponse({
                "errno": "400",
                "errmsg": "房源查找不到"
            })
        amount = days * house.price
        try:
            order = Order.objects.create(user_id=user.id,
                                         house_id=house_id,
                                         begin_date=start_date,
                                         end_date=end_date,
                                         days=days,
                                         house_price=house.price,
                                         amount=amount,
                                         status=1
                                         )
        except Exception as e:
            return JsonResponse({
                "errno": "400",
                "errmsg": "生成订单时出错"
            })
        return JsonResponse({
            "data": {
                "order_id": 1
            },
            "errno": "0",
            "errmsg": "下单成功"
        })

    def get(self, request):
        # role = json.loads(request.body.decode()).get('role')
        role = request.GET.get('role')

        user = request.user
        orders = Order.objects.filter(user=user)
        order_list = []
        for order in orders:
            order_list.append({
                "amount": order.amount,
                "comment": order.comment,
                "ctime": order.create_time,
                "days": order.days,
                "end_date": order.end_date,
                "img_url": "http://oyucyko3w.bkt.clouddn.com/FhgvJiGF9Wfjse8ZhAXb_pYObECQ",
                "order_id": order.id,
                "start_date": order.begin_date,
                "status": order.status,
                "title": "555"
            })
        return JsonResponse({
            "datas":{
                "orders":order_list
            },
            "errno": "0",
            "errmsg": "下单成功"
        })

# PUT api/v1.0/orders/[int:order_id]/status
class OrderStatus(LoginRequiredMixin, View):
    def put(self, request, house_id):
        result = json.loads(request.body)
        action = result.get('action')
        reason = result.get('reason')
        if action == 'accept':
            order = Order.objects.get(house_id= house_id)
            print(type(order))
            order.status = 3
            order.save()
            return JsonResponse({
                {
                    "errno": "0",
                    "errmsg": "操作成功"
                }
            })
        else:
            if not all([action, reason]):
                return JsonResponse({
                    "errno": "400",
                    "errmsg": "缺少必传参数"
                })
            order = Order.objects.get(house_id=house_id)
            order.status = 6
            order.save()
            return JsonResponse({
                "reason":reason,
                "errno": "0",
                "errmsg": "操作成功"
            })
